/** 
 * Debug.java
 * PROJECT 3: Sliding Block Puzzle Solver
 * @author Jonathan Kotker	cs61b-ar
 * @author Wei Zhong Yeh	cs61b-av
 */

import java.io.*;

/** 
 * Debug.java
 * PROJECT 3: Sliding Block Puzzle Solver
 * @author Jonathan Kotker
 * @author Wei Zhong Yeh 
 */

public class Debug {
	
	private static String fileName = "Debugging Output";
	private static boolean outputToFile = false;
	private static PrintStream outputStream;
	
	/**
	 *	No-argument constructor, which sets up the file output stream,
	 *	should it be used later in debugging
	 */
	public Debug ( ) {
		try {
			outputStream = new PrintStream (new FileOutputStream (new File (fileName)));
		} catch (FileNotFoundException e) {
			System.out.println ("Error in the creation of a file for debugging output");
		}
	}
	
	/**
	 * Sends debugging output to either the console or the file 
	 * @param string				String to be outputted, either to the console or the file
	 */
	public static void debugPrint (String string) {
		if (outputToFile)
			outputStream.println (string);
		else
			System.out.println (string);
	}

	/**
	 * Activates different debugging flags in different classes, based on the content of the string provided
	 * @param s			String containing keywords, signifying the debugging flags to be activated in varios classes
	 */
	public static void debugOption (String s) {
		// temporary variable to determine the position of the first comma in the String, if any
		int firstComma = s.indexOf (",");
		
		if (firstComma == -1) {
			if (s.equals ("options")) {
				System.out.println ("blockMoved \t Details every move of a single block in the tray");
				System.out.println ("traysGenerated \t Details the number of trays available in two main collections: one collection of trays yet to be seen, and another of trays already seen");
				System.out.println ("trayChosen \t Prints out the tray chosen for evaluation, and prints out its heuristic value (if any)");
				System.out.println ("moveStatistics \t Records useful statistics during the process of solution, such as time for complete solution, etc.");
				System.out.println ("outputToFile \t Channels the debugging output to a file called \'Debugging Output\' in the same directory as 'Solver'");
				System.out.println ("all \t Activates all of the above options, except \'outputToFile\', which must be added manually");
				System.out.println ("Notes: ");
				System.out.println ("* One or more of the above options can be activated by separating the keywords with a comma.");
				System.out.println ("* Generating the debugging strings can slow the functioning of the code down considerably.");
				System.exit (1);
			} else if (s.equals ("blockMoved")) {
				The.trackMovement ( );
			} else if (s.equals ("traysGenerated")) {
				The.displayContentsOfSet ( );
			} else if (s.equals ("trayChosen")) {
				The.viewChosenTrays ( );
			} else if (s.equals ("moveStatistics")) {
				The.findMovementStatistics ( );
			} else if (s.equals ("outputToFile")) {
				outputToFile = true;
			} else if (s.equals ("all")) {
				The.trackMovement ( );
				The.displayContentsOfSet ( );
				The.viewChosenTrays ( );
				The.findMovementStatistics ( );
			} else {
				System.out.println ("Invalid debugging keyword detected: " + s);
			}
		} else {
			debugOption (s.substring (0, firstComma));
			debugOption (s.substring (firstComma + 1));
		}
	}
}
